<?php

function cleanString($str, $lower=true) {
	//$search = array_map("utf8_decode", array('@[éèêëÊËÉÈ]@i','@[áãàâäÀÁÂÃÄÅ]@i','@[ìíîïÌÍÎÏ]@i','@[úûùüÙÚÛÜ]@i','@[òóõôöÒÓÔÕÖ]@i','@[ñÑ]@i','@[ýÿÝŸ]@i','@[çÇ]@i','@[œŒ]@i','@[ \'\-]@i','@[^a-zA-Z0-9_]@'));
	// $search = array('@[éèêëÊËÉÈ]@','@[áãàâäÀÁÂÃÄÅ]@','@[ìíîïÌÍÎÏ]@','@[úûùüÙÚÛÜ]@','@[òóõôöÒÓÔÕÖ]@','@[ñÑ]@','@[ýÿÝŸ]@','@[çÇ]@','@[œŒ]@','@[ \'\-]@','@[^a-zA-Z0-9_]@');
	// $replace = array ('e','a','i','u','o','n','y','c','oe','_','');
	// $string = preg_replace($search, $replace, $str);
	$string = str_replace(
			array(
				'á', 'ã', 'à', 'â', 'ä', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å',
				'é', 'è', 'ê', 'ë', 'Ê', 'Ë', 'É', 'È',
				'ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î', 'Ï',
				'ò', 'ó', 'õ', 'ô', 'ö', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö',
				'ú', 'û', 'ù', 'ü', 'Ù', 'Ú', 'Û', 'Ü',
				'ñ', 'Ñ',
				'ý', 'ÿ', 'Ý', 'Ÿ',
				'ç', 'Ç',
				'œ', 'Œ'
			),
			array(
				'a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'A', 'A',
				'e', 'e', 'e', 'e', 'E', 'E', 'E', 'E',
				'i', 'i', 'i', 'i', 'I', 'I', 'I', 'I',
				'o', 'o', 'o', 'o', 'o', 'O', 'O', 'O', 'O', 'O',
				'u', 'u', 'u', 'u', 'U', 'U', 'U', 'U',
				'n', 'N',
				'y', 'y', 'Y', 'Y',
				'c', 'C',
				'oe', 'OE'
			), $str);
	if (!$lower) {
		return $string;
	} else {
		return strtolower($string);
	}
}

class Setup_CrosserController extends Mac_Controller {

    public function init() {
        //$this->layout->addScript('jquery.js');
        $this->layout->addStylesheet('styles.css');
        $this->layout->setHeadTitle('Crosser Page Test');
    }

    public function indexAction() {
        $cpt = 0;
        Database_Mysql::dbSelect('crosser');
        Database_Mysql::connect()->manualSelect('SET NAMES \'utf8\'');
        Database_Mysql::setDebugMode(false);
        $lang = 1;
        
        $start = microtime(true);
        
        $words = file(ROOT_PATH . '/export/liste-mots-tagges.txt');
        foreach ($words as $word) {
            $cpt++;
            $split = explode("\t", $word);
            $mot_complet = trim($split[0]);
            if (mb_strlen(trim($mot_complet), 'utf8') > 1) {
                $mot_clean_letters = cleanString($mot_complet);
                $mot_clean = str_replace(array("'", ' ', '-'), array('', '', ''), $mot_clean_letters);
                //$mot_clean = str_replace(':', '', $mot_split);
                $mot_exist = Database_Mysql::connect()->existsID('dictionary', 'word', $mot_clean);
                if (!$mot_exist) {
                    Database_Mysql::connect()->insert('dictionary', array('word' => $mot_clean, 'length' => strlen($mot_clean)));
                    //$mot_split = str_replace(array("'", ' ', '-'), array(':', ':', ':'), $mot_clean_letters);
                    //$mot_clean = str_replace(':', '', $mot_split);
                    /*Database_Mysql::connect()->insert('word',
                        array(
                            'display_content' => $mot_complet,
                            'content' => $mot_clean,
                            'splitted_content' => $mot_split,
                            'length' => strlen($mot_clean)
                        )
                    );*/
                }
            }
        }
        
        $end = microtime(true);
        echo 'Time : '.(($end-$start)/1000);
        die;
        
        $mot = Database_Mysql::connect('dictionary')->getById('lemme', 'content', 'idlemme', 1);
        die($mot);
    }
}
